﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Dhtl.Service.IService;
using dai_hoc_tuong_lai.Areas.Admin.Models;
using DataBase.Daihoctuonglai;
using FarmeWork.Helper;
using Comom.Project.ControllerCom;
using Dhtl.Service.Export;

namespace dai_hoc_tuong_lai.Areas.Admin.Controllers
{
     [Authorize(Roles = "Admin")]
    public class EmailResController : UTController
    {
        //
        // GET: /Admin/EmailRes/

        private readonly IEmailDangky _iEmailDangky;
        private readonly IExport _iExport;
        private readonly IEmailSystem _iEmailSystem;
        private readonly EmailHelper emailCode = new EmailHelper();

        public EmailResController(IEmailDangky iEmailDangky, IExport iExport, IEmailSystem iEmailSystem)
        {
            this._iEmailDangky = iEmailDangky;
            this._iExport = iExport;
            this._iEmailSystem = iEmailSystem;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult List()
        {

            var listEntity = _iEmailDangky.List();
            List<EmailResModel> model = new List<EmailResModel>();
            try
            {
                foreach (var item in listEntity)
                {
                    EmailResModel ml = new EmailResModel();
                    EmailResModel.MapFrom(item, ref  ml);
                    model.Add(ml);
                }
            }
            catch { }

            return PartialView("_List", model);
        }

        public ActionResult Add()
        {
            EmailResModel model = new EmailResModel();
            model.id = 0;
            model.DateCreate = DateTime.Now;
            model.Status = true;
            
            return PartialView("_Add", model);
        }

        public ActionResult Update(int id)
        {
            EmailResModel model = new EmailResModel();
            var entity = _iEmailDangky.GetById(id);
            try
            {
                EmailResModel.MapFrom(entity, ref model);
            }
            catch { }

            return PartialView("_Update", model);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddOrUpdate(EmailResModel model)
        {

            try
            {
                if (string.IsNullOrEmpty(model.Email))
                {
                    return JsonError("Vui lòng điền Email");
                }

                if (!IsValidEmailAddress(model.Email))
                {
                    return JsonError("Email bạn nhập sai định dạng");
                }

                var entity = new EmailDangky();
                EmailResModel.MapFrom(model, ref entity);
                _iEmailDangky.AddOrUpdate(entity);

                return JsonSuccess(Url.Action("Index"), null);
            }
            catch (Exception ex)
            {
                return JsonError(ex.ToString());
            }
        }

        public ActionResult Delete(int id)
        {
            try
            {
                _iEmailDangky.delete(id);
                return JsonSuccess(Url.Action("Index"), null);
            }
            catch (Exception ex)
            {
                return JsonError(ex.ToString());
            }
        }


        public ActionResult ExportExcel()
        {
            try
            {
                var listVisa = _iEmailDangky.List();

                var fileName = string.Format("Danh-Sach-Email-Dang-ky_{0}.xlsx", DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss"));
                string filePath = System.IO.Path.Combine(Request.PhysicalApplicationPath, "Content\\files\\ExportImport", fileName);
                _iExport.ExportEmailResToXlsx(filePath, listVisa);
                var bytes = System.IO.File.ReadAllBytes(filePath);
                return File(bytes, "text/xls", fileName);
            }
            catch (Exception exc)
            {
                return RedirectToAction("Index");
            }
        }
        
    }
}
